package de.planethold.sevenloadj;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;


public class Utiliy {
	public static void printElement(Element element) {
		NodeList nodeList = element.getChildNodes();
		for (int i=0; i<nodeList.getLength(); i++) {
			if (nodeList.item(i).hasChildNodes()) {
				System.out.println(nodeList.item(i).getNodeName());
				printElement((Element)nodeList.item(i));
			} else {
				System.out.println(nodeList.item(i).getNodeName() + ": " + nodeList.item(i).getNodeValue());
			}
		}
	}
	
	public static String printList(List<String> list) {
		StringBuffer buffer = new StringBuffer();
		for (String tag: list) {
			buffer.append(tag + "\n");
		}
		return buffer.toString();
	}
	
	public static Calendar getCalendar(String createdAt) {
		//FIXME is this possible with SimpleDateFormat ???
		createdAt = createdAt.substring(0,22) + createdAt.substring(23);
		
		SimpleDateFormat dateAndTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
		dateAndTime.setLenient(false);
		Date date;
		try {
			date = (Date)dateAndTime.parse(createdAt);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			return cal;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public static String printCalendar(Calendar calendar) {
		SimpleDateFormat dateAndTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
		return dateAndTime.format(calendar.getTime());
	}
	
	public static List<String> createList(Element element, String name) {
		ArrayList<String> tags = new ArrayList<String>();
		NodeList nodeList = element.getElementsByTagName("tag");
		for (int i=0; i<nodeList.getLength(); i++) {
			tags.add(nodeList.item(i).getChildNodes().item(0).getNodeValue());
		}
		return tags;
	}

	public static boolean getBoolean(String attribute) {
		return attribute.equals("true");
	}
	
}
